<?php

class EmployeeManager extends BaseManager implements iManager {

	public function query() {
		$ret = true;
		
		$query = "SELECT 
					e.employee_id AS employee_id,
				    e.firstname AS firstname,
				    e.lastname AS lastname,
				    e.contact_number AS contact_number,
				    e.address AS `address`,
				    e.hired_date AS `hire_date`,
				    p.possition_name AS `position`,
				    es.status AS `status`,
				    a.access_id AS access_id,
				    a.admin AS admin,
				    a.pos AS pos,
				    a.stock AS stock,
				    a.reports AS reports,
				    a.complaints AS complaints
				FROM `employee_tbl` AS e
				LEFT JOIN `employee_account_tbl` AS ea 
					ON ea.employee_id = e.employee_id
				LEFT JOIN `possition_tbl` AS p
					ON p.possition_id = e.possition_id
				LEFT JOIN `access_tbl` AS a
					ON a.access_id = p.access_id
				LEFT JOIN `employment_status_tbl` AS es
					ON es.employment_status_id = e.employment_status
				WHERE e.`employee_id` = '" . $this->obj->getEmployeeId() . "' 
						AND `password` = '" . $this->obj->getPassword() . "' AND es.status != 'RESIGNED'";
		
		$this->db->query($query);
		
		$this->db->execute();
		
		$result = $this->db->getResultAssoc();
		
		$employee = new Employee_Domain();
		
		$employee->setEmployeeId($result['employee_id']);
		$employee->setFirstName($result['firstname']);
		$employee->setLastName($result['lastname']);
		$employee->setAddress($result['address']);
		$employee->setHiredate($result['hire_date']);
		$employee->setContactNumber($result['contact_number']);
		$employee->setPosition($result['position']);
		$employee->setStatus($result['status']);
		
		$accessLevel = new AccessLevel_Domain();
		
		$accessLevel->setAccessId($result['access_id']);
		$accessLevel->setAdmin($result['admin']);
		$accessLevel->setSales($result['pos']);
		$accessLevel->setRestock($result['stock']);
		$accessLevel->setReports($result['reports']);
		$accessLevel->setComplaints($result['complaints']);
		
		$employee->setAccessLevel($accessLevel);
		
		$loginDetails = new LoginDetails_Domain();
		
		$loginDetails->setLoginDateTime(date('F j, Y, g:i a'));
		
		$employee->setLoginDetails($loginDetails);

		$this->result = $employee;
	}
	
	public function add() {
		// add employee
		$data['firstname'] = $this->obj->getFirstName();
		$data['lastname'] = $this->obj->getLastName();
		$data['contact_number'] = $this->obj->getContactNumber();
		$data['address'] = $this->obj->getAddress();
		$data['hired_date'] = date('Y-m-d H:i:s',strtotime($this->obj->getHiredate()));
		$data['possition_id'] = $this->obj->getPosition();
		$data['branch_id'] = $this->obj->getBranch();
		$data['employment_status'] = '90001'; //default employed status
		
		$this->db->insert('employee_tbl', $data);
		$this->db->execute();

		
		// create login account
		$account['employee_id'] = $this->db->getLastId();
		$account['password'] = $this->obj->getLastName();
		$account['date_update'] = date('Y-m-d H:i:s');
		$this->db->insert('employee_account_tbl', $account);
		$this->db->execute();
		
		$this->result = $this->db->getAffectedRows();
		
	}
	
	public function delete() {
		
	}
	
	public function update() {
		//update employee info
		
		$where['employee_id'] = $this->obj->getEmployeeId();
		
		$data['firstname'] = $this->obj->getFirstName();
		$data['lastname'] = $this->obj->getLastName();
		$data['contact_number'] = $this->obj->getContactNumber();
		$data['address'] = $this->obj->getAddress();
		$data['hired_date'] = date('Y-m-d H:i:s',strtotime($this->obj->getHiredate()));
		$data['possition_id'] = $this->obj->getPosition();
		$data['branch_id'] = $this->obj->getBranch();
		$data['employment_status'] = $this->obj->getStatus();
		
		$this->db->update('employee_tbl', $data, $where);
		
		$this->db->execute();
		
		$this->result = $this->db->getAffectedRows();
		
	}
	
	public function getEmployees(){
		$results = array();
		
		$query = "SELECT * from `employee_tbl`";
		
		$this->db->query($query);
		
		$this->db->execute();
				
		while ($result = $this->db->getResultAssoc()) {
			$employee = new Employee_Domain();
		
			$employee->setEmployeeId($result['employee_id']);
			$employee->setFirstName($result['firstname']);
			$employee->setLastName($result['lastname']);
			$employee->setAddress($result['address']);
			$employee->setHiredate($result['hired_date']);
			$employee->setContactNumber($result['contact_number']);
			
			$results[] = $employee;
		}
		
		$this->result = $results;		
	}
	
	public function getEmployeeById(){
		$ret = true;
		
		$query = "SELECT 	`employee_id`, 
							`firstname`, 
							`lastname`, 
							`contact_number`, 
							`address`, 
							`hired_date`, 
							`possition_id`, 
							`branch_id`, 
							`employment_status`
							FROM `employee_tbl`
							WHERE `employee_id` = '" . $this->obj->getEmployeeId() . "'";
		
		$this->db->query($query);
		
		$this->db->execute();
		
		$result = $this->db->getResultAssoc();
		
		$employee = new Employee_Domain();
		
		$employee->setEmployeeId($result['employee_id']);
		$employee->setFirstName($result['firstname']);
		$employee->setLastName($result['lastname']);
		$employee->setAddress($result['address']);
		$employee->setHiredate($result['hired_date']);
		$employee->setContactNumber($result['contact_number']);
		$employee->setPosition($result['possition_id']);
		$employee->setBranch($result['branch_id']);
		$employee->setStatus($result['employment_status']);	
		
		$this->result = $employee;
	}
	
	public function updateAccount(){
		
		$where['employee_id'] = $this->obj->getEmployeeId();
		
		$data['password'] = $this->obj->getPassword();
		$data['date_update'] = date('Y-m-d H:i:s');
		
		$this->db->update('employee_account_tbl', $data, $where);
		
		$this->db->execute();
		
		$this->result = $this->db->getAffectedRows();
	}
}